package com.guigu.wenzhiyi.controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.guigu.aop.OpLog;
import com.guigu.limeng.pojo.Mdesignprocedure;
import com.guigu.limeng.pojo.Mdesignproceduredetails;
import com.guigu.limeng.pojo.Mdesignproceduremoduledetails;
import com.guigu.limeng.service.MdesignprocedureService;
import com.guigu.limeng.service.MdesignproceduredetailsService;
import com.guigu.limeng.service.MdesignproceduremoduledetailsService;
import com.guigu.liuzhireng.pojo.Account;
import com.guigu.liuzhireng.pojo.PageVo;
import com.guigu.tankai.pojo.Dfile;
import com.guigu.tankai.service.DfileService;
import com.guigu.util.service.IDService;
import com.guigu.wenzhiyi.pojo.Mapply;
import com.guigu.wenzhiyi.pojo.Mmanufacture;
import com.guigu.wenzhiyi.pojo.Mprocedure;
import com.guigu.wenzhiyi.pojo.Mproceduremodule;
import com.guigu.wenzhiyi.service.*;
import com.guigu.xiaowenbo.pojo.Sgather;
import com.guigu.xiaowenbo.pojo.Sgatherdetails;
import com.guigu.xiaowenbo.pojo.Spay;
import com.guigu.xiaowenbo.pojo.Spaydetails;
import com.guigu.xiaowenbo.service.SgatherService;
import com.guigu.xiaowenbo.service.SgatherdetailsService;
import com.guigu.xiaowenbo.service.SpayService;
import com.guigu.xiaowenbo.service.SpaydetailsService;
import com.mysql.cj.Session;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("Mmanufacture.do")
public class MmanufactureController {

    @Autowired
    MmanufactureService mmanufactureService;
    @Autowired
    MapplyService mapplyService;
    @Autowired
    DfileService dfileService;
    @Autowired
    MdesignproceduredetailsService mdesignproceduredetailsService;
    @Autowired
    MdesignproceduremoduledetailsService mdesignproceduremoduledetailsService;
    @Autowired
    MdesignprocedureService mdesignprocedureService;
    @Autowired
    MprocedureService mprocedureService;
    @Autowired
    MproceduringService mproceduringService;
    @Autowired
    MproceduremoduleService mproceduremoduleService;
    @Autowired
    SpayService spayService;
    @Autowired
    SpaydetailsService spaydetailsService;
    @Autowired
    SgatherService sgatherService;
    @Autowired
    SgatherdetailsService sgatherdetailsService;
    @Autowired
    IDService idService;

    @RequestMapping("WZYmManufactureAddCha")
    @ResponseBody
    public List<Mapply> WZYmapplyAddCha(Mapply mapply) {
        return mapplyService.WZY_mManufacture_Add_cha(mapply);
    }

    @RequestMapping("mManufactureWZY_tf")
    @ResponseBody
    public int mManufactureWZY_tf(String id) {
        if (dfileService.mManufactureWZY_tf(id) != null) {
            return 1;
        } else {
            return -1;
        }
    }

    @RequestMapping("DfileXQWZY")
    @ResponseBody
    public Dfile DfileXQWZY(String id) {
        return dfileService.DfileXQWZY(id);
    }

    @RequestMapping("mManufactureCha")
    @ResponseBody
    public Mdesignprocedure mManufactureCha(String id) {
        Mdesignprocedure mdesignprocedure = mdesignprocedureService.MdesignprocedureWZY_cha(id);
        List<Mdesignproceduredetails> list = mdesignproceduredetailsService.MdesignproceduredetailsWZY_AddCha(mdesignprocedure.getId());
        for (Mdesignproceduredetails md : list) {
            List<Mdesignproceduremoduledetails> mdesignproceduremoduledetails = mdesignproceduremoduledetailsService.MdesignproceduremoduledetailsWZY_AddCha(md.getId());
            md.setMdesignproceduremoduledetailsList(mdesignproceduremoduledetails);
        }
        mdesignprocedure.setMdesignproceduredetailsList(list);
        return mdesignprocedure;
    }

    @RequestMapping("mManufactureSHCha")
    @ResponseBody
    public Mmanufacture mManufactureSHCha(Integer id) {
        Mmanufacture mdesignprocedure = mmanufactureService.mManufactureWZY_SH_list(id);
        List<Mprocedure> mprocedures = mprocedureService.Mmanufacture_WZY_DengJi_SH_list_WanCheng(mdesignprocedure.getId());
        for (Mprocedure md : mprocedures) {
            List<Mproceduremodule> m = mproceduremoduleService.mManufactureWZY_SH_list(md.getId());
            md.setMpLists(m);
        }
        mdesignprocedure.setMprocedureList(mprocedures);
        return mdesignprocedure;
    }

    // 制定生产派工单
    @RequestMapping("mManufactureAdd")
    @OpLog(opModule = "生产调度管理模块",operType = "制定生产派工单",operDesc = "此方法用于制定生产派工单")
    public String mManufactureAdd(String mManufacture, String mProcedures, String ids) {
        String mManufactureID = idService.Work_OrDer_ID();
        Mmanufacture mmanufacture = new Gson().fromJson(mManufacture, Mmanufacture.class);
        mmanufacture.setManufactureid(mManufactureID);
        int i1 = mmanufactureService.insert(mmanufacture);

        List<Mprocedure> mprocedures = new Gson().fromJson(mProcedures, new TypeToken<List<Mprocedure>>() {
        }.getType());
        for (Mprocedure mp : mprocedures) {
            Mprocedure mp1 = mp;
            mp1.setParentid(i1);
            int i = mprocedureService.insert(mp1);
            for (Mproceduremodule ms : mp.getMpLists()) {
                Mproceduremodule mproceduremodule = ms;
                mproceduremodule.setParentid(i);
                mproceduremoduleService.insert(mproceduremodule);
            }
        }

        List<Integer> IDS = new Gson().fromJson(ids, new TypeToken<List<Integer>>() {
        }.getType());
        for (Integer id : IDS) {
            Mapply mapply = new Mapply();
            mapply.setId(id);
            mapply.setManufacturetag("P001-1");
            mapplyService.updateWZY_Mapply_ZT(mapply);
        }
        return "/wen_zhi_yi/mManufactureAdd.html";
    }

    @RequestMapping("mManufactureWZY_SH")
    @ResponseBody
    public List<Mmanufacture> mManufactureWZY_SH() {
        return mmanufactureService.mManufactureWZY_SH();
    }

    // 生产工序过程记录
    @RequestMapping("updateWZYSHZT")
    @OpLog(opModule = "生产调度管理模块",operType = "生产派工单审核",operDesc = "此方法用于生产派工单审核")
    public String updateWZYSHZT(String mManufacture) {
        Mmanufacture mmanufacture = new Gson().fromJson(mManufacture, Mmanufacture.class);
        mmanufactureService.updateWZY_SH_ZT(mmanufacture);
        return "/wen_zhi_yi/mManufactureSH.html";
    }

    // 出库 和 出库记录
    @RequestMapping("MmanufactureSpaySpaydetails")
    @OpLog(opModule = "出库管理模块",operType = "出库物料",operDesc = "此方法用于出库物料到内部生产")
    public void MmanufactureSpaySpaydetails(String spay, String slist) {
        int SL=0;
        List<Mproceduremodule> mmS = new ArrayList<>();
        List<Mprocedure> list = new Gson().fromJson(slist, new TypeToken<List<Mprocedure>>() {
        }.getType());
        Mprocedure mmSS = list.get(0);
        for (Mprocedure ms : list) {
            for (Mproceduremodule ms1 : ms.getMpLists()) {
                SL+=ms1.getAmount();
                mmS.add(ms1);
            }
        }
        List<Mproceduremodule> mmSSS = new ArrayList<>();
        for (Mproceduremodule ss : mmSS.getMpLists()) {
            Mproceduremodule mm02 = ss;
            int shu = 0;
            int zongXJ = 0;
            for (Mproceduremodule mm : mmS) {
                if (ss.getProductid().equals(mm.getProductid())) {
                    shu += mm.getAmount();
                    zongXJ += mm.getSubtotal();
                }
            }
            mm02.setAmount(shu);
            mm02.setSubtotal(zongXJ);
            mmSSS.add(mm02);
        }

        String ID = idService.Stock_Out_ID();
        Spay spay1 = new Gson().fromJson(spay, Spay.class);
        spay1.setPayid(ID);
        spay1.setAmountsum(SL);
        int i = spayService.insert_WZY(spay1);
        for (Mproceduremodule sss : mmSSS) {
            Spaydetails spaydetails = new Spaydetails();
            spaydetails.setParentid(i);
            spaydetails.setProductid(sss.getProductid());
            spaydetails.setProductname(sss.getProductname());
            spaydetails.setAmount(sss.getAmount());
            spaydetails.setSubtotal(sss.getSubtotal());
            spaydetails.setPaidamount(sss.getAmount());
            spaydetails.setPaytag("K002-1");
            spaydetailsService.insert_WZY(spaydetails);
        }
    }

    @RequestMapping("MmanufactureWZYCha")
    @ResponseBody
    public List<Mmanufacture> MmanufactureWZYCha(Mmanufacture mmanufacture) {
        return mmanufactureService.MmanufactureWZY_Cha(mmanufacture);
    }

    @RequestMapping("mManufactureWZYSHlist")
    @ResponseBody
    public List<Mprocedure> mManufactureWZYSHlist(Integer id) {
        List<Mprocedure> list = mprocedureService.mManufactureWZY_SH_list(id);
        List<Mprocedure> mprocedures = list;
        for (Mprocedure mprocedure : list) {
            Mprocedure mprocedure1 = mprocedure;
            List<Mproceduremodule> list1 = mproceduremoduleService.mManufactureWZY_SH_list(mprocedure.getId());
            mprocedure1.setMpLists(list1);
        }
        return mprocedures;
    }

    @RequestMapping("MmanufactureWZYDengJiChaPage")
    @ResponseBody
    public PageVo<Mmanufacture> MmanufactureWZYDengJiChaPage(@RequestBody PageVo<Mmanufacture> pageVo) {
        return mmanufactureService.MmanufactureWZY_DengJi_Cha_Page(pageVo.getNum(), pageVo.getSize(), pageVo.getObj());
    }

    // 内部生产
    @RequestMapping("MmanufactureDengJi")
    @OpLog(opModule = "内部生产管理模块",operType = "生产登记",operDesc = "此方法用于生产登记")
    public String MmanufactureDengJi(String realLabourHourAmount, String mManufacture, String mProcedure, String shu) {
        Mmanufacture mmanufacture = new Gson().fromJson(mManufacture, Mmanufacture.class);
        Mprocedure mprocedure = new Gson().fromJson(mProcedure, Mprocedure.class);
        List<Integer> amounts = new Gson().fromJson(shu, new TypeToken<List<Integer>>() {
        }.getType());
        int wuliaoXJ = 0;
        // 生产工序物料表
        for (int i = 0; i < mprocedure.getMpLists().size(); i++) {
            Mproceduremodule mproceduremodule = new Mproceduremodule();
            mproceduremodule.setId(mprocedure.getMpLists().get(i).getId());
            mproceduremodule.setRealamount(amounts.get(i));
            int zhi = mprocedure.getMpLists().get(i).getCostprice() * amounts.get(i);
            mproceduremodule.setRealsubtotal(zhi);
            wuliaoXJ += zhi;
            mproceduremoduleService.updateWXY(mproceduremodule);
        }
        // 生产工序表
        Mprocedure mprocedure1 = new Mprocedure();
        mprocedure1.setId(mprocedure.getId());
        mprocedure1.setReallabourhouramount(Integer.parseInt(realLabourHourAmount));
        mprocedure1.setRealsubtotal(Integer.parseInt(realLabourHourAmount) * mprocedure.getCostprice());
        mprocedure1.setRealmodulesubtotal(wuliaoXJ);
        mprocedure1.setProcedurefinishtag("G004-2");
        mprocedure1.setProceduretransfertag("G005-0");
        mprocedureService.updateWXY(mprocedure1);
        // 生产总表
        Mmanufacture mmanufacture1 = new Mmanufacture();
        mmanufacture1.setId(mmanufacture.getId());
        mmanufacture1.setManufactureproceduretag("S002-1");
        mmanufactureService.updateWZY(mmanufacture1);
        return "/wen_zhi_yi/mManufactureInsideAdd(1).html";
    }

    // 内部生产审核 Cha
    @RequestMapping("MmanufactureWZYDengJiSHPage")
    @ResponseBody
    public PageVo<Mmanufacture> MmanufactureWZYDengJiSHPage(@RequestBody PageVo<Mmanufacture> pageVo) {
        return mmanufactureService.MmanufactureWZY_DengJi_SH_Page(pageVo.getNum(), pageVo.getSize(),pageVo.getObj());
    }

    @RequestMapping("MmanufactureWZYDengJiSHlist")
    @ResponseBody
    public List<Mprocedure> MmanufactureWZYDengJiSHlist(Integer id) {
        List<Mprocedure> list = mprocedureService.mManufactureWZY_SH_list(id);
        List<Mprocedure> mprocedures = list;
        for (Mprocedure mprocedure : list) {
            Mprocedure mprocedure1 = mprocedure;
            List<Mproceduremodule> list1 = mproceduremoduleService.mManufactureWZY_SH_list(mprocedure.getId());
            mprocedure1.setMpLists(list1);
        }
        return mprocedures;
    }

    // 内部生产 审核
    @RequestMapping("MmanufactureDengJiSH")
    @OpLog(opModule = "内部生产管理模块",operType = "生产登记审核",operDesc = "此方法用于生产登记审核")
    public String MmanufactureDengJiSH(String realLabourHourAmount, String mManufacture, String mProcedure, String shu) {
        Mmanufacture mmanufacture = new Gson().fromJson(mManufacture, Mmanufacture.class);
        Mprocedure mprocedure = new Gson().fromJson(mProcedure, Mprocedure.class);
        List<Integer> amounts = new Gson().fromJson(shu, new TypeToken<List<Integer>>() {
        }.getType());
        int wuliaoXJ = 0;
        // 生产工序物料表
        for (int i = 0; i < mprocedure.getMpLists().size(); i++) {
            Mproceduremodule mproceduremodule = new Mproceduremodule();
            mproceduremodule.setId(mprocedure.getMpLists().get(i).getId());
            mproceduremodule.setRealamount(amounts.get(i));
            int zhi = mprocedure.getMpLists().get(i).getCostprice() * amounts.get(i);
            mproceduremodule.setRealsubtotal(zhi);
            wuliaoXJ += zhi;
            mproceduremoduleService.updateWXY(mproceduremodule);
        }
        // 生产工序表
        Mprocedure mprocedure1 = new Mprocedure();
        mprocedure1.setId(mprocedure.getId());
        mprocedure1.setReallabourhouramount(Integer.parseInt(realLabourHourAmount));
        mprocedure1.setRealsubtotal(Integer.parseInt(realLabourHourAmount) * mprocedure.getCostprice());
        mprocedure1.setRealmodulesubtotal(wuliaoXJ);
        mprocedure1.setProcedurefinishtag("G004-1");
        mprocedure1.setProceduretransfertag("G005-0");
        mprocedureService.updateWXY(mprocedure1);
        // 生产总表
        Mmanufacture mmanufacture1 = new Mmanufacture();
        mmanufacture1.setId(mmanufacture.getId());
        //实际  工时总成本 和  物料总成本
        List<Mprocedure> list = mprocedureService.mManufactureWZY_SH_list(mmanufacture.getId());
        int gongshi = 0;
        int wuliao = 0;
        for (Mprocedure m : list) {
            gongshi+=(m.getRealsubtotal()==null?0:m.getRealsubtotal());
            wuliao+=(m.getRealmodulesubtotal()==null?0:m.getRealmodulesubtotal());
        }
        mmanufacture1.setReallabourcostpricesum(gongshi);
        mmanufacture1.setRealmodulecostpricesum(wuliao);

        mmanufacture1.setManufactureproceduretag("S002-1");
        mmanufactureService.updateWZY(mmanufacture1);
        return "/wen_zhi_yi/mManufactureInsideSH.html";
    }

    // 内部生产 交接
    @RequestMapping("MmanufactureDengJiAddJJ")
    @OpLog(opModule = "内部生产管理模块",operType = "生产交接登记",operDesc = "此方法用于生产交接登记合格数量")
    public String MmanufactureDengJiAddJJ(Integer testedamount,Integer mmfid,Integer mid) {
        Mprocedure mprocedure = new Mprocedure();
        mprocedure.setId(mid);
        mprocedure.setProceduretransfertag("G005-2");
        mprocedureService.updateJJ(mprocedure);

        Mmanufacture mmanufacture = new Mmanufacture();
        mmanufacture.setId(mmfid);
        mmanufacture.setTestedamount(testedamount);
        mmanufacture.setManufactureproceduretag("S002-1");
        mmanufactureService.updateWZY(mmanufacture);
        return "/wen_zhi_yi/mManufactureInsideAdd(1).html";
    }

    // 内部生产审核 交接复核
    @RequestMapping("MmanufactureDengJiAddSHJJ")
    @OpLog(opModule = "内部生产管理模块",operType = "生产交接登记审核和入库产品",operDesc = "此方法用于生产交接登记审核和入库产品数量")
    public String MmanufactureDengJiAddSHJJ(Integer testedamount, Integer mmfid, Integer mid, HttpSession session) {
        // 修改 工序状态
        Mprocedure mprocedure = new Mprocedure();
        mprocedure.setId(mid);
        mprocedure.setProceduretransfertag("G005-1");
        mprocedureService.updateJJ(mprocedure);

        // 修改 投产数量 和 合格数量
        Mmanufacture mmanufacture = new Mmanufacture();
        mmanufacture.setId(mmfid);
        mmanufacture.setTestedamount(testedamount);
        // 生产工序审核 是否 全部通过 (修改状态)
        List<Mprocedure> mpZong = mprocedureService.Mmanufacture_WZY_DengJi_SH_list(mmanufacture.getId());
        System.out.println(mpZong.size());
        if (mpZong.size() == 0) {
            mmanufacture.setManufactureproceduretag("S002-2");
            mmanufactureService.updateWZY(mmanufacture);

            // 产品入库 和 入库记录
            Mmanufacture shList = mmanufactureService.mManufactureWZY_SH_list(mmfid);
            Account user = (Account) session.getAttribute("user");
            String ID = idService.Stock_In_ID();
            // 产品入库
            Sgather sgather= new Sgather();
            sgather.setGatherid(ID);
            sgather.setStorer(user.getAccount());
            sgather.setAmountsum(shList.getAmount());
            sgather.setGatheredamountsum(shList.getTestedamount());
            sgather.setCostpricesum(shList.getRealmodulecostpricesum()+shList.getReallabourcostpricesum());
            sgather.setRegister(user.getAccount());
            int i = sgatherService.insert_WZY(sgather);
            // 入库记录
            Sgatherdetails sgatherdetails = new Sgatherdetails();
            sgatherdetails.setParentid(i);
            sgatherdetails.setProductid(shList.getProductid());
            sgatherdetails.setProductname(shList.getProductname());
            sgatherdetails.setProductescribe(shList.getProductdescribe());
            sgatherdetails.setAmount(mmanufacture.getTestedamount());
            sgatherdetails.setAmountunit("内部生产部");
            sgatherdetails.setCostprice(shList.getTestedamount());
            sgatherdetails.setSubtotal(shList.getRealmodulecostpricesum()+shList.getReallabourcostpricesum());
            sgatherdetails.setGatheredamount(shList.getTestedamount());
            sgatherdetailsService.insert_WZY(sgatherdetails);
        }else {
            mmanufacture.setManufactureproceduretag("S002-1");
            mmanufactureService.updateWZY(mmanufacture);
        }
        return "/wen_zhi_yi/mManufactureInsideSH.html";
    }

    // 内部生产查询 Cha
    @RequestMapping("MmanufactureWZYChaPage")
    @ResponseBody
    public PageVo<Mmanufacture> MmanufactureWZYChaPage(@RequestBody PageVo<Mmanufacture> pageVo) {
        return mmanufactureService.MmanufactureWZY_Cha_Page(pageVo.getNum(), pageVo.getSize(),pageVo.getObj());
    }





}
